<?php
declare (strict_types=1);

namespace app\school\logic\school\admin;

use app\admin\logic\system\Log;
use app\common\exception\LogicException;
use app\school\model\school\admin\SchoolLevel as SchoolLevelModel;

/**
 * 机构等级管理逻辑层
 */
class SchoolLevel extends SchoolLevelModel
{
    /**
     * 获取列表
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-05-21
     *
     * @param    [type]                         $map 条件
     * @return   [type]                              [description]
     */
    public function getList(array $map = [])
    {
        return $this->where($map)->order('update_time', 'DESC')->paginate($this->getListRows());
    }

    /**
     * 创建等级
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-05-22
     *
     * @param array $data [description]
     * @return   [type]                               [description]
     */
    public function createLevel(array $data)
    {
        $check = [
            'title'           => $data['title'],
            'min_sales_price' => $data['min_sales_price'],
            'proportion'      => $data['proportion'],
        ];

        // 验证数据
        $this->getValidate()->scene('create')->failException()->check($check);

        // 创建成功
        if (!$this->save($check)) {
            throw new LogicException('新建失败');
        }

        Log::write('新建机构等级【' . $check['title'] . '】', 'school', 'create', true);

        return $this;
    }

    /**
     * 编辑等级
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-05-22
     *
     * @param    [type]                         $id   [description]
     * @param    [type]                         $data [description]
     * @return   [type]                               [description]
     */
    public function editLevel($id, $data)
    {

        $level = $this->where('id', $id)->findOrFail();

        $check       = $data;
        $check['id'] = $id;
        // 验证数据
        $this->getValidate()->scene('edit')->failException()->check($check);

        $level->title           = $data['title'];
        $level->min_sales_price = $data['min_sales_price'];
        $level->proportion      = $data['proportion'];
        if (!$level->save()) {
            throw new LogicException('修改失败');
        }

        Log::write('编辑机构等级【' . $data['title'] . '】', 'school', 'update', true);

        return $level;
    }

    /**
     * 删除等级
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-05-22
     *
     * @param    [type]                         $id [description]
     * @return   [type]                             [description]
     */
    public function deleteLevel($id)
    {
        $level = $this->where('id', $id)->findOrFail();

        if (!$level->delete()) {
            throw new LogicException("删除失败");
        }

        Log::write('删除机构等级【' . $level->title . '】', 'school', 'delete', true);
    }

    /**
     * 获取等级选项
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-05-27
     *
     * @return   [type]                         [description]
     */
    public function getSelectOptions()
    {
        return $this->field(['id' => 'value', 'title'])->select()->toArray();
    }

    /**
     * 获取达到销售额最低的等级ID
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/6/28
     *
     * @return mixed
     */
    public function getMinLevelId()
    {
        return $this->order('min_sales_price', 'ASC')->value('id');
    }
}
